package com.maomao.maomaochong_i.productPackage;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.maomao.maomaochong_i.R;
import com.maomao.maomaochong_i.adapterPackage.ViewPagerAdaper;

import java.util.ArrayList;

/**
 * Created by maomao on 2017/5/17.
 */

public class ProductMessage extends AppCompatActivity implements View.OnClickListener,
        ViewPager.OnPageChangeListener{

    private Context context;
    private ViewPager vpager;
    private ImageView line;
    private TextView tv_one;
    private TextView tv_two;
    private TextView tv_three;
    private TextView tv_four;
    private TextView tv_five;
    private TextView tv_six;
    private TextView select;
    private LinearLayout layout;
    private View imgview1, imgview2, imgview3;
    private PhotoPopupWindow pw;
//    private ImageView imageView;

    private ArrayList<View> listViews;
    private int offset = 0;//下划线的偏移量
    private int currIndex = 0;//当前页面的编号
    private int bmpWidth;// 下划线的长度
    private int one = 0; //下划线滑动一页的距离
    private int two = 0; //滑动条移动两页的距离

    private String sstr;

    private int[] images = {R.mipmap.beauty01, R.mipmap.beauty02, R.mipmap.beauty03};

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.produce_message);
        context = ProductMessage.this;
        initViews();
    }

    private void initViews() {
        vpager = (ViewPager) findViewById(R.id.imageViewPager);
        tv_one = (TextView) findViewById(R.id.textView4);
        tv_two = (TextView) findViewById(R.id.textView5);
        tv_three = (TextView) findViewById(R.id.textView6);
        tv_four = (TextView) findViewById(R.id.textView7);
        tv_five = (TextView) findViewById(R.id.textView8);
        tv_six = (TextView) findViewById(R.id.textView9);
        select = (TextView) findViewById(R.id.select);
        layout = (LinearLayout) findViewById(R.id.layout);
        line = (ImageView) findViewById(R.id.lin1);



        //下划线动画的相关设置
        bottomLine();

        //往ViewPager填充View，同时设置点击事件与页面切换事件
        listViews = new ArrayList<View>();
        LayoutInflater mInflater = getLayoutInflater();
        imgview1 =mInflater.inflate(R.layout.product_picture1, null, false);
        imgview2 =mInflater.inflate(R.layout.product_picture2, null, false);
        imgview3 =mInflater.inflate(R.layout.product_picture3, null, false);
        listViews.add(imgview1);
        listViews.add(imgview2);
        listViews.add(imgview3);

        vpager.setAdapter(new ViewPagerAdaper(listViews));
        vpager.setCurrentItem(0);
        vpager.addOnPageChangeListener(this);

        tv_one.setOnClickListener(this);
        tv_two.setOnClickListener(this);
        tv_three.setOnClickListener(this);
        tv_four.setOnClickListener(this);
        tv_five.setOnClickListener(this);
        tv_six.setOnClickListener(this);
        select.setOnClickListener(this);
        layout.setOnClickListener(this);
        tv_one.performClick();

        sstr = "选购(颜色、尺寸)";
        select.setText(sstr);
    }

    //下划线动画的相关设置
    private void bottomLine(){
        // 获取下划线宽度
        bmpWidth = BitmapFactory.decodeResource(getResources(), R.mipmap.lin1).getWidth();
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        // 获取分辨率宽度
        int screenWidth = dm.widthPixels;
        // 计算偏移量
        offset = (screenWidth / 3 - bmpWidth) / 2;
        // 移动一页的偏移量,比如1->2,或者2->3
        one = offset * 2 + bmpWidth;
        // 移动两页的偏移量,比如1直接跳3
        two = one * 2;

        Matrix matrix = new Matrix();
        matrix.postTranslate(offset, 0);
        // 设置动画初始位置
        line.setImageMatrix(matrix);
    }

    //文本颜色
    public void allHideColor(){
        tv_one.setTextColor(0xff000000);
        tv_two.setTextColor(0xff000000);
        tv_three.setTextColor(0xff000000);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.textView4:
                allHideColor();
                tv_one.setTextColor(0xfff94b4c);
                vpager.setCurrentItem(0);
                break;

            case R.id.textView5:
                allHideColor();
                tv_two.setTextColor(0xfff94b4c);
                vpager.setCurrentItem(1);
                break;

            case R.id.textView6:
                allHideColor();
                tv_three.setTextColor(0xfff94b4c);
                vpager.setCurrentItem(2);
                break;

            case R.id.select:
                pw = new PhotoPopupWindow(context, select);
                pw.showAtLocation(pw.useView(), Gravity.BOTTOM, 0, 0);
                break;

            case R.id.textView9:
                if (select.getText().toString() != sstr)
                    Toast.makeText(ProductMessage.this,"已加入购物车",Toast.LENGTH_SHORT).show();
                else
                    Toast.makeText(ProductMessage.this,"请选择商品属性",Toast.LENGTH_SHORT).show();
                break;
        }
    }

    @Override
    public void onPageSelected(int index) {
//        listViews.get(index).setBackgroundResource(images[index]);

        Animation animation = null;
        switch (index) {
            case 0:
                if (currIndex == 1) {
                    animation = new TranslateAnimation(one, 0, 0, 0);
                } else if (currIndex == 2) {
                    animation = new TranslateAnimation(two, 0, 0, 0);
                }
                allHideColor();
                tv_one.setTextColor(0xfff94b4c);
                break;
            case 1:
                if (currIndex == 0) {
                    animation = new TranslateAnimation(offset, one, 0, 0);
                } else if (currIndex == 2) {
                    animation = new TranslateAnimation(two, one, 0, 0);
                }
                allHideColor();
                tv_two.setTextColor(0xfff94b4c);
                break;
            case 2:
                if (currIndex == 0) {
                    animation = new TranslateAnimation(offset, two, 0, 0);
                } else if (currIndex == 1) {
                    animation = new TranslateAnimation(one, two, 0, 0);
                }
                allHideColor();
                tv_three.setTextColor(0xfff94b4c);
                break;
        }
        currIndex = index;
        animation.setFillAfter(true);// true表示图片停在动画结束位置
        animation.setDuration(300); //设置动画时间为300毫秒
        line.startAnimation(animation);//开始动画
    }

    @Override
    public void onPageScrollStateChanged(int i) {

    }

    @Override
    public void onPageScrolled(int i, float v, int i1) {

    }

    private class MyViewPagerAdapter extends PagerAdapter {
        public int getCount() {
            return listViews.size();
        }

        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0==arg1;
        }

        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(listViews.get(position));
            return listViews.get(position);
        }

        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(listViews.get(position));
        }
    }
}

